import { useCallback } from "react";
import { useSelector, useDispatch } from "react-redux";
import productApi from "../api/product";
import Creators from "../store/actions";

export const useProduct = () => {

  const product = useSelector((state) => state.getProduct);
  const dispatch = useDispatch();
  const { curPage, eachPage } = product;
  //获取学生数据
  const getProduct = useCallback(async () => {
    const data = await productApi.get({ curPage, eachPage });
    console.log(data);
    dispatch(Creators.getProduct(data));
  }, [curPage, eachPage, dispatch]);
  //修改当前页
  //修改当前页
  const setCurPage = useCallback(
    (curPage) => {
      dispatch(Creators.setCurPage({ curPage }));
    },
    [dispatch]
  );
  //修改每页条数
  const setEachPage = useCallback(
    (eachPage) => {
      dispatch(Creators.setEachPage({ eachPage }));
    },
    [dispatch]
  );
  //获取某一条学生数据
  const searchProduct = useCallback(async (data) => {
    dispatch(Creators.searchProduct(data));
  }, [dispatch]);
  return [product, { getProduct, setCurPage, setEachPage,searchProduct }];
};
